package com.exam.service.impl;

import com.exam.mapper.QuestionMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Paging;
import com.exam.pojo.Question;
import com.exam.pojo.Subject;
import com.exam.service.ClassesService;
import com.exam.service.MajorService;
import com.exam.service.QuestionService;
import com.exam.service.SubjectService;
import com.exam.util.GradeUtil;
import com.exam.util.PropertiesUtil;
import com.exam.util.QuestionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: 黄星
 * 题目服务实现类
 */

@Service("questionServiceImpl")
public class QuestionServiceImpl implements QuestionService {

	@Resource(name = "questionMapper")
	private QuestionMapper questionMapper;
	@Resource(name = "subjectServiceImpl")
	private SubjectService subjectService;
	@Resource(name = "classesServiceImpl")
	private ClassesService classesService;
	@Resource(name = "paging")
	Paging<Question> paging;

	@Resource
	private ApplicationContext ctx;

	public List<Question> getChoiceQuestionBySubjectName(String subjectName) {

		return getQuestion( subjectName, 1, PropertiesUtil.getQuestionNumber( "choice" ) );

	}

	public List<Question> getBlankQuestionBySubjectName(String subjectName) {

		return getQuestion( subjectName, 2,  PropertiesUtil.getQuestionNumber( "blank" ) );

	}


	public List<Question> getJudgeQuestionBySubjectName(String subjectName) {

		List<Question> questions = getQuestion( subjectName, 3, PropertiesUtil.getQuestionNumber( "judge" ) );
		Random random = new Random();
		for (int i = 0; i < questions.size(); i++) {
			int chocie = random.nextInt( 2 );
			Question question = questions.get( i );
			//显示统一用topic属性来显示
			if (chocie == 0) {
				//为0  就选取正确题目来显示 ， 并设置答案为对
				question.setAnswer( "对" );
			} else if (chocie == 1) {
				//为1  就选取错误题目来显示  ， 并设置答案为错
				question.setTopic( question.getErrAnswer() );
				question.setAnswer( "错" );
			}
		}
		return questions;

	}

	/**
	 * @param subjectName 科目类别
	 * @param typeId      题目类型
	 * @param number      抽取题目数量
	 *                    私有方法  获取题目有重复代码，就提取成一个方法
	 */
	private List<Question> getQuestion(String subjectName, int typeId, int number) {

		Random random = new Random();   //用于随机下标//要生成选择题的数目
		List<Question> questions = questionMapper.getQuestionBySubjectName( subjectName, typeId );
		List<Integer> indexs = new ArrayList<Integer>();           //存储已获取问题的下标
		List<Question> returnQuestions = new ArrayList<Question>();   //存储生成的问题列表

		for (int i = 0; i < number; i++) {
			int index = random.nextInt( questions.size() );

			while (QuestionUtils.HasApperedIndex( index, indexs )) {
				//进入循环说明该下标有出现过，就重新生成下标
				index = random.nextInt( questions.size() );
			}
			indexs.add( index );
			returnQuestions.add( questions.get( index ) );   //往下标组，跟题目组添加数据
		}
		return returnQuestions;
	}

	public List<List<String>> getChoiceQuestionAnswerByRandom(List<Question> questions) {

		List<List<String>> answerList = new ArrayList<List<String>>();
		for (int i = 0; i < questions.size(); i++) {
			Question question = questions.get( i );
			answerList.add( QuestionUtils.randomAnswer( question.getAnswer(), question.getErrAnswer() ) );
		}

		return answerList;
	}

	public List<List<String>> getBlankQuestionAnswerBySplit(List<Question> questions) {

		List<List<String>> splitQuestionList = new ArrayList<List<String>>();

		for (int i = 0; i < questions.size(); i++) {
			Question question = questions.get( i );
			splitQuestionList.add( QuestionUtils.splitTopic( question.getTopic() ) );
		}

		return splitQuestionList;
	}

	public Paging<Question> questionPaging(Map map) {
		Map param = new HashMap(); //参数列表
		Map<String, Object> selCountMap = new HashMap<String, Object>();
		Integer pageSize = (Integer) map.get( "pageSize" );
		Integer page = (Integer) map.get( "page" );
		List<Question> questionList = null;
		Integer totalCount = 0;

		Integer majorId = (Integer) map.get( "majorId" );
		Integer gradeId = (Integer) map.get( "gradeId" );
		Integer typeId = (Integer) map.get( "typeId" );
		Integer subjectId = (Integer) map.get( "subjectId" );

		List<Classes> classes = classesService.getClassesByMajorId( majorId );  //获取到该专业的所有班级
		String claasesName = GradeUtil.getGrade( gradeId );
		Integer classesId = 0;
		for(int i = 0;i < classes.size();i++){
			if(classes.get( i ).getClassesName().contains( claasesName  )){
				classesId = classes.get( i ).getId();
				break;
			}
		}

		if(gradeId == 0){
			selCountMap.put( "classesId",-1 );
			param.put( "classesId",-1 );
		}else {
			selCountMap.put( "classesId",classesId );
			param.put( "classesId",classesId );
		}

		selCountMap.put( "majorId",majorId );
		selCountMap.put( "subjectId", subjectId);
		selCountMap.put( "typeId", typeId);
		selCountMap.put( "status" , map.get( "status" ) );
		//填充参数列表
		param.put( "start", ((Integer) map.get( "page" ) - 1) * (Integer) map.get( "pageSize" ) );
		param.put( "offset", map.get( "pageSize" ) );
		param.put( "majorId",majorId );
		param.put( "subjectId", subjectId);
		param.put( "typeId", typeId);
		param.put( "status" , map.get( "status" ) );


		totalCount = questionMapper.getTotalCount( selCountMap );
		questionList = questionMapper.getQuestionByPaging( param );
		paging.setTotalCount( totalCount ); //设置总记录数
		paging.setCurrentPage( page ); //设置当前页
		paging.setPageSize( pageSize ); //设置页面大小
		paging.setList( questionList );

		if (totalCount % pageSize == 0) {
			paging.setTotalPage( totalCount / pageSize );
		} else {
			paging.setTotalPage( (totalCount / pageSize) + 1 );
		}
		return paging;
	}

	public Question getQuestionById(Integer questionId) {
		return questionMapper.getQuestionById( questionId );
	}

	public Integer delQuestionById(Integer questionId) {
		return questionMapper.delQuestionById( questionId );
	}

	public Integer updQuestion(Map map) {
		return questionMapper.updQuestion( map );
	}

	public Integer addQuestion(Map map) {
		return questionMapper.addQuestion( map );
	}

	public Integer recoveryQuestion(Integer questionId) {
		return questionMapper.recoveryQuestion( questionId );
	}

}
